<?php
namespace Home\Controller;
use Think\Controller;
class StoreController extends CommonController {
    public function index(){
        $condition['ecm_store.store_id'] = array('neq',2);
        $cond['condition'] = $condition;
        $con = array(
            'condition' => $condition ,
            'order' => 'add_time desc'
        );
        $count = D('store')->getStoreList($con);
        $page = I('p') ? I('p') : '1';
        $pageSize = I('pageSize') ? I('pageSize') : '15';
        $condition['ecm_store.store_id'] = array('neq',2);
        $cond['condition'] = $condition;
        $cond = array(
            'condition' => $condition ,
            'page' => $page.','.$pageSize,
            'order' => 'add_time desc'
        );
        $res = D('store')->getStoreList($cond);
        $this->assign('store',$res);
        $Page       = new \Think\Page(count($count),$pageSize);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('header','条留言');

        $Page->setConfig('first','首页');

        $Page->setConfig('prev','上一页');

        $Page->setConfig('next','下一页');

        $Page->setConfig('last','尾页');

        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%<p id="pageTips">第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pageSize.' 条/页 共 %TOTAL_ROW% 条信息)</p>');
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
    	$this->display();
    }

    public function changeAccount(){
        $res = D('account')->editAccountInfo(array('account_id'=>$_POST['id']),array('recommend_state'=>$_POST['recommend_state']));
        if($res === false){
            $return_array = array(
                'code' => '10010',
                'msg' => '修改状态失败'
            );
            echo json_encode($return_array);
            exit;
        }else{
            $return_array = array(
                'code' => '10000',
                'msg' => '修改状态成功'
            );
            echo json_encode($return_array);
            exit;
        }
    }

    public function insert(){
        if(!$_POST){
            $res = M('region')->where(array('parent_id'=>0))->select();
            $this->assign('region1',$res);
            $this->display();
        }else{
            D('member')->startTrans();
            $data_member = array(
                'user_name' => $_POST['user_name'],
                'password' => md5($_POST['password']),
                'phone_mob' => $_POST['phone_mob'],
                'email' => $_POST['email'],
                'reg_time' => time()
            );
            $res_member = D('member')->insertSellerInfo($data_member);
            if(!$res_member){
                $return_array = array(
                    'code' => '10010',
                    'msg' => '添加用户失败'
                );
                D('member')->rollback();
                echo json_encode($return_array);
                exit;
            }
            $data_store = array(
                'store_id' => $res_member,
                'store_name' => $_POST['store_name'],
                'region_id' => $_POST['region_id'],
                'address' => $_POST['address'],
                'region_name' => trim($_POST['region_name']),
                'state' => 1,
                'add_time' => time(),
            );
            $res_store = D('store')->insertStoreInfo($data_store);
            if(!$res_store){
                $return_array = array(
                    'code' => '10010',
                    'msg' => '添加用户失败'
                );
                D('member')->rollback();
                echo json_encode($return_array);
                exit;
            }
            D('member')->commit();
            $this->redirect(U('Store/index'));
        }
    }
    public function revise(){
        if(!$_POST){
            if (!$_GET['store_id']){
                return false;
            }
            $res = D('store')->getStoreInfo(array('ecm_store.store_id'=>$_GET['store_id']));
            $this->assign('store',$res);
            $resRegion = M('region')->where(array('parent_id'=>0))->select();
            $this->assign('region1',$resRegion);
            $this->display();
        }else{
            if (!$_POST['store_id']){
                return false;
            }
            D('member')->startTrans();
            $data_member = array(
                'user_name' => $_POST['user_name'],
                'phone_mob' => $_POST['phone_mob'],
                'email' => $_POST['email'],
            );
            $res_member = D('member')->reviseSellerInfo(array('user_id'=>$_POST['store_id']),$data_member);
            if($res_member  === false){
                $return_array = array(
                    'code' => '10010',
                    'msg' => '修改用户失败'
                );
                D('member')->rollback();
                echo json_encode($return_array);
                exit;
            }
            $data_store = array(
                'store_id' => $_POST['store_id'],
                'store_name' => $_POST['store_name'],
                'region_id' => $_POST['region_id'],
                'address' => $_POST['address'],
                'region_name' => trim($_POST['region_name']),
                'state' => $_POST['state'],
            );
            $res_store = D('store')->reviseStoreInfo(array('store_id'=>$_POST['store_id']),$data_store);
            if($res_store === false){
                $return_array = array(
                    'code' => '10010',
                    'msg' => '修改店铺失败'
                );
                D('member')->rollback();
                echo json_encode($return_array);
                exit;
            }
            D('member')->commit();
            $this->redirect(U('Store/index'));
        }
    }

    public function getRegion(){
        if(!$_POST['parent_id']){
            $return_array = array(
                'code' => '10010',
                'msg' => '参数错误'
            );
            echo json_encode($return_array);
            exit;
        }
        $res = M('region')->where(array('parent_id'=>$_POST['parent_id']))->select();
        if(!$res){
            $return_array = array(
                'code' => '10020',
                'msg' => '获取城市列表失败'
            );
            echo json_encode($return_array);
            exit;
        }else{
            $return_array = array(
                'code' => '10000',
                'msg' => '获取城市列表成功',
                'data' => $res
            );
            echo json_encode($return_array);
            exit;
        }
    }

}